const err = require("../constant/err.type.js");
const { saveConfigs, getConfigs } = require("../services/configs.service.js");

class ConfigsController {
  async getConfig(ctx, next) {
    var config = ctx.request.body;
    var res;
    try {
      res = await getConfigs(config);
      if (!res) {
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: -1, msg: "获取配置错误", data: "" }),
          ctx
        );
      } else {
        ctx.status = 200;
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: 0, msg: "ok", data: res }),
          ctx
        );
      }
    } catch (error) {
      console.log(error);
      ctx.app.emit(
        "error",
        err.setDefaultValue({
          code: -1,
          msg: "获取配置错误" + error,
          data: "",
        }),
        ctx
      );
    }
  }
  async saveConfig(ctx, next) {
    var config = ctx.request.body;
    var res;
    try {
      res = await saveConfigs(config);
      if (!res) {
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: -1, msg: "保存配置错误", data: "" }),
          ctx
        );
      } else {
        ctx.status = 200;
        ctx.app.emit(
          "error",
          err.setDefaultValue({ code: 0, msg: "ok", data: res }),
          ctx
        );
      }
    } catch (error) {
      console.log(error);
      ctx.app.emit(
        "error",
        err.setDefaultValue({
          code: -1,
          msg: "获取配置错误" + error,
          data: "",
        }),
        ctx
      );
    }
  }
}

module.exports = new ConfigsController();
